package com.yue.http.httpcore.message;

import org.apache.http.*;
import org.apache.http.entity.StringEntity;
import org.apache.http.message.BasicHttpRequest;
import org.apache.http.message.BasicHttpResponse;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

/**
 * 功能描述: TODO <br/>
 * 创建时间: 2016/10/9<br/>
 *
 * @author QiuYang
 */
public class MassageDemo {
    public static void main(String[] args) throws Exception {
        requestMessage();
        responseMessage();
//        entity();
    }

    /**
     * Http Request Message
     */
    public static void requestMessage(){
        HttpRequest request = new BasicHttpRequest("GET", "/", HttpVersion.HTTP_1_1);

        System.out.println(request.getRequestLine().getMethod());
        System.out.println(request.getRequestLine().getUri());
        System.out.println(request.getProtocolVersion());
//        System.out.println(request.getRequestLine().getProtocolVersion());

//        System.out.println(request.toString());
        System.out.println(request.getRequestLine().toString());
    }

    /**
     * Http response message
     */
    public static void responseMessage(){
        HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK");

        StatusLine sl = response.getStatusLine();
        System.out.println(sl.getProtocolVersion());
//        System.out.println(response.getProtocolVersion());
        System.out.println(sl.getStatusCode());
        System.out.println(sl.getReasonPhrase());

        System.out.println(sl.toString());
    }

    /**
     * Http message common properties and methods
     */
    public static void responseProperties(){

    }

    /**
     * Http Entity
     */
    public static void entity() throws IOException {
        StringEntity myEntity = new StringEntity("important message中国", Consts.UTF_8);

        System.out.println(myEntity.getContentType());
        System.out.println(myEntity.getContentLength());
        System.out.println(EntityUtils.toString(myEntity));
        System.out.println(EntityUtils.toByteArray(myEntity).length);
    }
}
